﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using My_App.Model;


namespace My_App
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            this.Items = new ObservableCollection<Task>();
        }

        ObservableCollection<Task> _item;

        public ObservableCollection<Task> Items
        {
            get
            {
                return _item;
            }
            set
            {
                if (value != _item)
                {
                    _item = value;
                    NotifyPropertyChanged("Items");
                }
            }
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        public void LoadData()
        {
            if ( MainPage.My_IsolaledStorage[MainPage.task_string] == null)
            {

                this.Items.Add(new Task() { Name = "Завдання №1", Description = "Вивчити SQL", IsSelected = true });
                this.Items.Add(new Task() { Name = "Завдання №2", Description = "Поїсти круасани", });
                this.Items.Add(new Task() { Name = "Завдання №3", Description = "Попити каву", });
            }
            else 
            {
                Items = (ObservableCollection<Task>)MainPage.My_IsolaledStorage[MainPage.task_string];
            }
            this.IsDataLoaded = true;
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}